﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ClassLibrary.DAO;
using ClassLibrary.Entity;
using ClassLibrary.Data;
using System.IO;
using System.Text;


public partial class Admin_UserGuide : System.Web.UI.Page
{
    TblLayoutCodeDAO LayoutCodeDAO = new TblLayoutCodeDAO(Global.CONNECTION_STRING);
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadLayoutById();
        }
    }

    private void LoadLayoutById()
    {
        string idLayout = Request.QueryString["idLayout"];
        TblLayoutCode Layout = LayoutCodeDAO.GetLayoutByID(Convert.ToInt32(idLayout));
        txtLayoutName.Text = Layout.LayoutName;
        txtCodeDetails.Value = Layout.CodeDetails;
        if (Layout.Display == true)
            rdDisplay1.Checked = true;
        if (Layout.Display == false)
            rdDisplay2.Checked = true;
        imgGuide.ImageUrl = "~/Admin/img/Layout/" + Layout.ImageURL;
    }
    protected void save_Click(object sender, EventArgs e)
    {
        bool bFlag = false;
        string idLayout = Request.QueryString["idLayout"];
        try
        {
            TblLayoutCode layout = new TblLayoutCode();
            layout.LayoutName = txtLayoutName.Text;
            layout.CodeDetails = txtCodeDetails.Value;

            if (rdDisplay2.Checked == true)
                layout.Display = false;
            if (rdDisplay1.Checked == true)
                layout.Display = true;
            layout.IdLanguage = 1;
            layout.ViewCount = 0;
            string filename = f_name.FileName;

            string path = Server.MapPath("~\\Admin\\img\\Layout\\");


            if (idLayout == null || idLayout == "")
            {
                if (File.Exists(path + "\\" + filename))
                {
                    lb_message.Text = "Trùng hình ảnh";
                    return;
                }
                f_name.SaveAs(path + "\\" + filename);

                layout.IDLayout = 0;
                layout.ImageURL = filename;
                LayoutCodeDAO.InsUpdLayoutCode(layout, 1);
            }
            else
            {
                layout.IDLayout = Convert.ToInt32(idLayout);
                string fname = layout.ImageURL;
                if (fname != null)
                {
                    File.Delete(Server.MapPath("~\\Admin\\img\\Layout\\" + fname));
                }
                layout.ImageURL = filename;
                f_name.SaveAs(Server.MapPath("~\\Admin\\img\\Layout\\" + filename));

                LayoutCodeDAO.InsUpdLayoutCode(layout, 2);
            }

            bFlag = true;
        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect(Global.ERROR_PAGE_ADMIN);
        }
        if (bFlag)
            Response.Redirect("~/Admin/ListLayout.aspx");
    }
}
